package net.jueb.frameworkDemo.orm.mongodb.driver;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import org.bson.Document;
import org.bson.codecs.configuration.CodecRegistries;
import org.bson.codecs.configuration.CodecRegistry;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoClientURI;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

import net.jueb.frameworkDemo.orm.enums.RoleSexEnum;
import net.jueb.frameworkDemo.orm.enums.RoleStatusEnum;

public class DriverTest {
	MongoClient mongo;
	MongoDatabase dataBase;
	
	public DriverTest(String host,int port,String dataBaseName,String user,String pwd,boolean isAdminUser) {
		MongoClientOptions.Builder builder=initConfig();
		//创建认证信息
//		MongoCredential auth=null;
//		MongoClientURI uri=null;//uri方式认证
//		if(isAdminUser)
//		{
//			auth=MongoCredential.createCredential(user, "admin", pwd.toCharArray());
//			uri = new MongoClientURI("mongodb://"+user+":"+pwd+"@"+host+":"+port+"/?authSource=admin",builder);
//		}else
//		{
//			auth=MongoCredential.createCredential(user, dataBaseName, pwd.toCharArray());
//			uri = new MongoClientURI("mongodb://"+user+":"+pwd+"@"+host+":27017/?authSource="+dataBaseName,builder);
//		}
//		mongo=new MongoClient(uri);//uri获取客户端
		//集群方式连接
		mongo=new MongoClient(new MongoClientURI("mongodb://192.168.0.73:27017,"
				+ "192.168.0.73:27018,"
				+ "192.168.0.73:27019/?replicaSet=rs0&slaveOk=true&readPreference=secondaryPreferred",builder));//uri获取客户端
//		mongo=new MongoClient(new ServerAddress(host),Arrays.asList(auth),builder.build());//获取客户端
		dataBase=mongo.getDatabase(dataBaseName);//获取目标数据库
		initCodec();
	}
	
	protected MongoClientOptions.Builder initConfig()
	{
		MongoClientOptions.Builder build = new MongoClientOptions.Builder();  
		//与数据最大连接数10  
		build.connectionsPerHost(10);  
		//如果当前所有的connection都在使用中，则每个connection上可以有50个线程排队等待  
		build.threadsAllowedToBlockForConnectionMultiplier(10);  
		build.connectTimeout(1*60*1000);  
		build.maxWaitTime(60*60*1000);  
		return build;
	}
	
	protected void initCodec()
	{
		//注册枚举类型编码解码器集合
		CodecRegistry enumCodecRegistry=CodecRegistries.fromCodecs(
				new EnumCodec<RoleSexEnum>(RoleSexEnum.class),
				new EnumCodec<RoleStatusEnum>(RoleStatusEnum.class));
		//默认的解码器集合
		CodecRegistry defaultCodecRegistry=MongoClient.getDefaultCodecRegistry();
		//合并后的解码器集合
		CodecRegistry newCodecRegistry = CodecRegistries.fromRegistries(enumCodecRegistry,defaultCodecRegistry); 
		//设置解码器集合
		dataBase=dataBase.withCodecRegistry(newCodecRegistry);
	}
	
	public void init()
	{
		MongoCollection<Document> collection=dataBase.getCollection("roles");
		if(collection!=null)
		{
			collection.drop();
		}
		dataBase.createCollection("roles");
	}
	
	public void test1(int count)
	{
		MongoCollection<Document> collection=dataBase.getCollection("roles");
		long time=System.nanoTime();
		for(int i=0;i<count;i++)
		{
			Document dc=getTestDocument(i);
			collection.insertOne(dc);
		}
		time=System.nanoTime()-time;
		System.out.println("插入"+count+"条耗时:"+time+"纳秒,"+TimeUnit.NANOSECONDS.toMillis(time)+"毫秒");
		//创建索引
		collection.createIndex(new Document("roleId", 1));
		time=System.nanoTime();
		Document result=collection.find(new Document("roleId", 1000)).first();
		time=System.nanoTime()-time;
		System.out.println("查询"+count+"条 耗时:"+time+"纳秒,"+TimeUnit.NANOSECONDS.toMillis(time)+"毫秒,result:"+result);
	}
	
	public void test2(int count)
	{
		MongoCollection<Document> collection=dataBase.getCollection("roles");
		List<Document> dcs=new ArrayList<Document>();
		for(int i=0;i<count;i++)
		{
			Document dc=getTestDocument(i);
			dcs.add(dc);
		}
		long time=System.nanoTime();
		collection.insertMany(dcs);
		time=System.nanoTime()-time;
		System.out.println("插入"+count+"条耗时:"+time+"纳秒,"+TimeUnit.NANOSECONDS.toMillis(time)+"毫秒");
		//创建索引
		collection.createIndex(new Document("roleId", 1));
		time=System.nanoTime();
		Document result=collection.find(new Document("roleId", 1000)).first();
		time=System.nanoTime()-time;
		System.out.println("查询"+count+"条 耗时:"+time+"纳秒,"+TimeUnit.NANOSECONDS.toMillis(time)+"毫秒,result:"+result);
	}
	
	public Document getTestDocument(int index)
	{
		Document dc=new Document();
		dc.append("name", "测试角色"+index);
		dc.append("index",index);
		dc.append("roleId",1000+index);
		dc.append("pn","local");
		dc.append("money",1000000);
		dc.append("diamond",1000000);
		dc.append("phoneMoney",1000+index);
		dc.append("level",1000+index);
		dc.append("exp",1000+index);
		dc.append("rechargeAmount",1000+index);
		dc.append("sex",RoleSexEnum.male);
		dc.append("pwd",1000+index);
		dc.append("cellPhone",1000+index);
		dc.append("status",RoleStatusEnum.Enable);
		dc.append("faceIcon",1000+index);
		dc.append("isRobot",false);
		dc.append("maxBetId",1000+index);
		dc.append("currentCannon",1000+index);
		Set<Integer> cannons=new HashSet<Integer>();
		cannons.add(1);
		cannons.add(2);
		dc.append("cannons",cannons);
		dc.append("monthCardTime",1000+index);
		dc.append("lastMonthCardTime",1000+index);
		dc.append("createTime",new Date());
		dc.append("lastLogin",new Date());
		return dc;
	}
	
	public static void main(String[] args) {
		String host="192.168.0.207";
		int port=27017;
		String dataBaseName="h5fish_center";
		String user="h5fish";
		String pwd="123456";
		DriverTest t=new DriverTest(host,port,dataBaseName,user,pwd,false);
		t.init();
		t.test2(10);
		t.test2(1000);
		t.test2(10000);
		t.test2(10);
		t.test2(1);
	}
}
